<template>
  <div>
    <!-- 搜索 -->
    <!-- 质量报告状态维护 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="100" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :dataSource="dataList" :datakey="'records'"  :fetchapi="fetchApi" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange" :isSelectColumn="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleDetail" :title="'质量报告状态维护明细'" destroyOnClose width="70%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editForm @close="closeDetail" @save="saveDetail" :rowData="rowData"></editForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <ModelVehicle @close="close" @save="save"></ModelVehicle>
    </BaseDialog>
  </div>
</template>
<script>
  import editForm from './editForm.vue';
  import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
  import {getListByPage,getListForExport1,exporturl} from '@/api/claim/factory/QualityInformationManagement_OEM/QualityInformationExamine';
  import { getPartType } from '@/api/dictionary/index';
  import { exportExcelFile } from '@/utils/index';
  import { dictionary } from '@/utils/dictMixin';
  import moment from "moment";
  import { getAppId } from '@/assets/js/auth';

  export default {
    mixins: [dictionary],
    components: {
      editForm,
      ModelVehicle
    },
    data() {
      return {
        dataList: [],
        visible: false,
        visibleDrawer: false,
        collapse: true,
        infoId: null,
        topFilterInit: this.topFilterInitFun(),
        flterValue: {}, //参数
        columns: this.creatHeader(),
        fetchApi: getListByPage,
        fetchParams: {
          status : 3,
          xhrAbort: true
        }, //ipa 接口
        selectedRows: [], //选中经销商
        visibleDetail: false,
        visibleEdit: false,
        rowData:{}
      };
    },
    created() {
      this.fetchParams.beginCreatedAt = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
      this.fetchParams.endCreatedAt = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
      this.chuqi();
    },
    methods: {
      async chuqi() {
        console.log("进来了")
        // const res3 = await getPartType({type: 5563});
        // if (res3.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'caseLevel').itemList = res3.data.map(y => ({
        //     text: y.codeCnDesc,
        //     value: y.codeId
        //   }));
        // }
        // const res2 = await getPartType({type: 5561});
        // if (res2.resultCode == 200) {
        //   delete res2.data[0];
        //   delete res2.data[1];
        //   delete res2.data[2];
        //   delete res2.data[3];
        //   delete res2.data[4];
        //   this.topFilterInit.find(x => x.fieldName == 'reportStatus').itemList = res2.data.map(y => ({
        //     text: y.codeCnDesc,
        //     value: y.codeId
        //   }));
        // }
      },
      //搜索数据
      topFilterInitFun() {
        return [
          {
            type: 'INPUT',
            label: '车型',
            fieldName: 'modelName',
            readonly: true, // 只读
            unitRender: p => {
              return (
                <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()}/>
              </div>
            );
            }
          },
          { type: 'RANGE_DATE', label: '案例创建日期', fieldName: 'beginCreatedAt|endCreatedAt', placeholder: '请输入',
            initialValue: [
              moment()
                .startOf('week')
                .format('YYYY-MM-DD HH:mm:ss'),
              moment()
                .endOf('week')
                .format('YYYY-MM-DD HH:mm:ss')
            ],
            rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
          },
          {
            type: 'SELECT',
            label: '故障等级',
            fieldName: 'caseLevel',
            itemList: this.createDictList('5563'),
            readonly: true // 只读
          },
          { type: 'INPUT', label: '案例编号', fieldName: 'caseNo', placeholder: '请输入', initialValue: '' },
          { type: 'INPUT', label: '案例主题', fieldName: 'caseTitle', placeholder: '请输入', initialValue: '' },
          { type: 'RANGE_DATE', label: '更新日期', fieldName: 'beginUpdatedAt|endUpdatedAt', placeholder: '请输入', initialValue: '' },
          { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
          { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
          {
            type: 'SELECT',
            label: '处理状态',
            fieldName: 'reportStatus',
            itemList: this.createDictList('5561',[55611001,55611002,55611003,55611004,55611005]),
          },
        ];
      },
      //搜索获取数据
      changeHandle(val) {
        if(!val.beginCreatedAt) {
          this.$notify.warning({ title: '提示信息', message: '案例创建开始日期不可为空！' });
          return
        }
        if(!val.endCreatedAt) {
          this.$notify.warning({ title: '提示信息', message: '案例创建结束日期不可为空！' });
          return
        }
        if(getAppId() == 'arcfox') {
          if (moment(moment(val.endCreatedAt).format('YYYY-MM-DD')).diff(moment(val.beginCreatedAt), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
            return
          }
        } else {
          if (moment(moment(val.endCreatedAt).format('YYYY-MM-DD')).diff(moment(val.beginCreatedAt), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
            return
          }
        }
        this.fetchParams = Object.assign({}, this.fetchParams, val,{
          status : 3,xhrAbort: false
        });
        console.log(val);
      },
      //table表头和数据源
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            fixed: 'left',
            render: (props, h) => {
              return (
                <div>
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
              查看
              </multiuse-button>
              </div>
            );
            }
          },
          { title: '案例编号', dataIndex: 'caseNo', sorter: true },
          { title: '案例主题', dataIndex: 'caseTitle', sorter: true },
          { title: '处理状态', dataIndex: 'statusStr', sorter: true },
          { title: '案例创建日期', dataIndex: 'createdDate', sorter: true },
          { title: '案例申请日期', dataIndex: 'applyDate', sorter: true },
          { title: '车型', dataIndex: 'caseModelName', sorter: true },
          { title: '故障现象代码', dataIndex: 'caseFaultCode', sorter: true },
          { title: '故障现象名称', dataIndex: 'caseFaultName', sorter: true },
          { title: '故障等级', dataIndex: 'levelStr', sorter: true },
          { title: '故障次数', dataIndex: 'faultTimes', sorter: true },
          { title: '更新日期', dataIndex: 'updatedDate', sorter: true }
        ];
      },
      //选中
      selectChange(rows) {
        this.selectedRows = rows;
        console.log(this.selectedRows);
      },
      modelTurnOn() {
        this.visible = true;
      },
    async  exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000160' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
        // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginCreatedAt|endCreatedAt").initialValue[0];
        // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginCreatedAt|endCreatedAt").initialValue[1];
        // if(!beginSubmitDate) {
        //   this.$notify.warning({ title: '提示信息', message: '案例创建开始日期不可为空！' });
        //   return
        // }
        // if(!endSubmitDate) {
        //   this.$notify.warning({ title: '提示信息', message: '案例创建结束日期不可为空！' });
        //   return
        // }
        // if(getAppId() == 'arcfox') {
        //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
        //     return
        //   }
        // } else {
        //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
        //     return
        //   }
        // }
        // let obj = Object.assign({}, this.fetchParams, { reportStatus : 55611007,xhrAbort: false });
        // obj.beginCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginCreatedAt|endCreatedAt").initialValue[0];
        // obj.endCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginCreatedAt|endCreatedAt").initialValue[1];
        // getListForExport1(obj).then(res => {
        //   exportExcelFile(res.data, '质量报告状态维护');
        // });
      },
      closeDetail() {
        this.visibleDetail = false;
      },
      saveDetail() {
        this.visibleDetail = false;
        this.fetchParams = { ...this.fetchParams };
      },
      close() {
        this.visible = false;
      },
      save(obj) {
        this.visible = false;
        this.topFilterInit.find(x => x.fieldName == 'modelName').initialValue = obj[0].modelName;
      },
      //开启编辑
      edit(val) {
        this.rowData = val;
        this.visibleDetail = true;
      }
    }
    // mounted() {
    //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
    // }
  };
</script>

<style lang="less" scoped></style>
